$(function () {
    $("#jqGrid").Grid({
        url: '../otcplatformrecord/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '用户id', name: 'userid', index: 'userid', width: 80,hidden:true},
			{label: '昵称', name: 'nickname', index: 'nickname', width: 80,align:'center'},
			{label: '手机号码', name: 'phone', index: 'phone', width: 80,align:'center'},
			{label: '提链地址', name: 'address', index: 'address', width: 80,align:'center'},
			{label: '提链数量', name: 'count', index: 'count', width: 80,align:'center'},
			{label: '手续费', name: 'poundage', index: 'poundage', width: 80,align:'center'},
			{label: '到账数量', name: 'realcount', index: 'realcount', width: 80,align:'center'},
			{label: '提链状态', name: 'status', index: 'status', width: 80,align:'center',
				formatter:function (value) {
					if(value==0){
						return "提链中";
					}else if(value==1){
						return "提链完成";
					}else if(value==2){
						return "提链失败";
					}
				}
			},
			{label: '类型', name: 'type', index: 'type', width: 80,align:'center',
				formatter:function (value) {
					if(value==0){
						return "提链";
					}else if(value==1){
						return "充链";
					}
				}
			},
			{label: '提链时间', name: 'addTime', index: 'add_time', width: 80,align:'center',
				formatter:function (value) {
					return transDate(value, 'yyyy-MM-dd hh:mm:ss');
				}
			},
			{label: '修改时间', name: 'updateTime', index: 'update_time', width: 80,align:'center',hidden:true}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		otcPlatformRecord: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
			type:'',
			status:''
		},
		platforms:[],
		users:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.otcPlatformRecord = {};
			vm.queryPlatformLsit();
			vm.queryUserLsit();
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
			vm.queryPlatformLsit();
			vm.queryUserLsit();
		},
		saveOrUpdate: function (event) {
            let url = vm.otcPlatformRecord.id == null ? "../otcplatformrecord/save" : "../otcplatformrecord/update";
            vm.otcPlatformRecord.userid =$('#userselect option:selected').text();
            vm.otcPlatformRecord.platformId = $('#platformselect option:selected').text();
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.otcPlatformRecord),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../otcplatformrecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../otcplatformrecord/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.otcPlatformRecord = r.otcPlatformRecord;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'type':vm.q.type,'status':vm.q.status},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
				type:'',
				status:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
		queryPlatformLsit:function () {
			Ajax.request({
				url: "../otcplatform/queryAll",
				async: true,
				successCallback: function (r) {
					vm.platforms = r.list;
				}
			});
		},
		queryUserLsit:function () {
			Ajax.request({
				url: "../sportsuser/queryAll",
				async: true,
				successCallback: function (r) {
					vm.users = r.list;
				}
			});
		}
	}
});